//
//  ClassifyViewDetailController.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/8.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ClassifyViewDetailController.h"
#import "NetWorkingManager.h"
#import "ZJSqlit.h"
#import "ZJRefurbish.h"
/** 视频、音频cell */
#import "ZJStudyVideoTableViewCell.h"
/** 图书cell */
#import "ZJStudyBookTableViewCell.h"
#define videoCell @"ZJStudyVideoTableViewCell"
#define bookCell  @"ZJStudyBookTableViewCell"
@interface ClassifyViewDetailController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** dataArray */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 数据库 */
@property (nonatomic, strong) ZJSqlit *sqlitManager;
/** 刷新插件 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;

@end

@implementation ClassifyViewDetailController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor viewBGGrayColor];
    self.titlelabel.text = self.classInfo.CATEGORY_NAME;
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
        [self.tableView.mj_header beginRefreshing];
    [self setupUI];
    
}

- (void)setupUI{
    [self.view addSubview:self.tableView];
}

- (void)netWorking_header{
    NSDictionary *dict = @{@"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",_page],@"SHOW_NUMBER":@"15",@"CATEGORYID":self.classInfo.CATEGORY_ID};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL listCoursewareByCategory] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//请求成功
            
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:object[@"COURSEWARE_IN_CATEGORY"]] mutableCopy];

            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
}

 - (void)netWorking_footer{
     NSDictionary *dict = @{@"PAGE_NUMBER":[NSString stringWithFormat:@"%ld",_page],@"SHOW_NUMBER":@"15",@"CATEGORYID":self.classInfo.CATEGORY_ID};
     __weak typeof(self) weakSelf = self;
     [NetWorkingManager postWithUrlString:[NetWorkingURL listCoursewareByCategory] Parameter:dict Success:^(id object) {
         NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
         if ([model.STATE isEqualToString:@"20000"]) {//请求成功
             NSArray *array = [NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:object[@"COURSEWARE_IN_CATEGORY"]];
             [weakSelf.dataArray addObjectsFromArray:array];
             [weakSelf.tableView reloadData];
         }
         [weakSelf.tableView.mj_footer endRefreshing];
     } Fail:^(id object) {
         [weakSelf.tableView.mj_footer endRefreshing];
     }];
     
 }




#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudyRecommendModel * model = self.dataArray[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]) {//悦读会
        ZJStudyBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bookCell forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }else{
        
        ZJStudyVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:videoCell forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudyRecommendModel *model = self.dataArray[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
        return 125 * [UIScreen zj_screenHScale];//图书
    }
    return 120 * [UIScreen zj_screenHScale];//视频、音频
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    StudyCourseViewController *vc = [StudyCourseViewController new];
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter


#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height  - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 7.5 * [UIScreen zj_screenHScale])];
        view.backgroundColor = [UIColor viewBGGrayColor];
        _tableView.tableHeaderView = view;
        [_tableView  registerClass:[ZJStudyVideoTableViewCell class] forCellReuseIdentifier: videoCell];
        [_tableView  registerClass:[ZJStudyVideoTableViewCell class] forCellReuseIdentifier: bookCell];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}

- (ZJSqlit *)sqlitManager{
    if (_sqlitManager == nil){
        _sqlitManager = [ZJSqlit new];
    }
    return _sqlitManager;
}

- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
